//------------------------------------------------------------------------------
#include <QSqlQuery>
#include <QSqlRecord>
#include <QtDebug>
#include <QCryptographicHash>
#include "CGlobal.h"
#include "CChamp.h"
#include "CMySQL.h"
//------------------------------------------------------------------------------
static QString _token;
static QString _currentUser;
static QVariantMap _config;
//------------------------------------------------------------------------------
bool CGlobal::getChampValue(QMainWindow *window, QString tableName, QString champName, QString& value) {
	QList<QWidget *> objects = window->findChildren<QWidget *>();
	
	foreach(QWidget *object, objects) {
		CChamp * champ=dynamic_cast<CChamp *>(object);
		if(champ != NULL) {
			if(champ->getTable() == tableName && champ->getChamp() == champName) {
				value=champ->getValue();
				return true;
			}
		}
	}
	return false;
}
//------------------------------------------------------------------------------
QString CGlobal::addSlashesQuot(QString str) {
	return str.replace("\"", "\\\"");
}
//------------------------------------------------------------------------------
QString CGlobal::stripSlashesQuot(QString str) {
	return str.replace("\\\"", "\"");
}
//------------------------------------------------------------------------------
QString CGlobal::md5Hash(QString str) {
	return QString(QCryptographicHash::hash(str.toUtf8(),QCryptographicHash::Md5).toHex());
}
//------------------------------------------------------------------------------
QString CGlobal::getToken(void) {
	return _token;
}
//------------------------------------------------------------------------------
void CGlobal::setToken(QString token) {
	_token=token;
}
//------------------------------------------------------------------------------
QVariantMap CGlobal::getConfig(void) {
	return _config;
}
//------------------------------------------------------------------------------
void CGlobal::setConfig(QVariantMap config) {
	QMapIterator<QString, QVariant> i(config);
	
	while (i.hasNext()) {
		i.next();
		_config.insert(i.key(), i.value());
	}
}
//------------------------------------------------------------------------------
QString CGlobal::getCurrentUser(void) {
	return _currentUser;
}
//------------------------------------------------------------------------------
void CGlobal::setCurrentUser(QString currentUser) {
	_currentUser=currentUser;
}
//------------------------------------------------------------------------------